var djTest = {

    suiteName : "datajoin Test Suite",
	
    setUp: function () {
		
		djTest.Source = {
			
			text : 'Blake',
			
			textalign : 'center',
			
			checkbox : true,
			
			radio : true,
			
			h1 : 'The most awesome plugin',
			
			textarea : 'Some random text',
			
			select : 'one',
			
			select1 : 'one',
			
			select2 : 'B',
			
			multiselect : ['two', 'four'],
			
			phone : {
					
					cell : '555-555-5555',
					
					work : '012-345-6789'
			},
			
			a : 'http://gotmonkey.com',
			
			anchor : 'http://jquery.com'
		};
		
		djTest.Override = {
			
			obj: {
			
				text: {
				
					FieldAssign: function(field, source) {
					
						field.val(source.text);
						
						field.css('text-align', source.textalign);
					},
					
					SourceAssign: function(field, source, event) {
					
						source.text = field.val();
						
					}
					
				},
				
				select: {
				
					FieldAssign: function(field, source) {
					
						field.find('option:contains(' + source.select + ')').attr('selected', true);
					},
					
					SourceAssign: function(field, source, event) {
					
						source.select = field.find('option:selected').text();
						
					}
					
				},
				
				phone: {
				
					work: {
					
						FieldAssign: function(field, source) {
						
							field.val('***-***' + source.phone.work.substr(7));
							
						},
						
						SourceAssign: function(field, source, event) {
						
							source.phone.work = field.val();
							
							djTest.Override.obj.phone.work.FieldAssign(field, source);
							
						}
						
					}
				
				}
				
			},
			
			tag : {
				
				select : {
					
					FieldAssign : function( field, obj, property ){
						
						field.find('option:contains(' + obj[property] + ')').attr('selected', true);
						
					},
					SourceAssign : function( field, obj, property, event ){
						
						obj[property] = field.find('option:selected').text();
						
					}
					
					
				}
				
			}
			
		};
		
    },
	
	testSetTextField : function () {
		
		var input = $('<input type="text" data-join="text" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		assertEqual(input.val(), djTest.Source.text, 'Input field value should match "text" property value of source object.');
	},
	
	testSetCheckBoxField  : function () {
		
		var input = $('<input type="checkbox" data-join="checkbox" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		assertEqual(input.attr('checked'), djTest.Source.checkbox, 'CheckBox should match "checkbox" property value of source object.');
		
	},
	
	testSetRadioField  : function () {
		
		var input = $('<input type="radio" data-join="radio" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		assertEqual(input.attr('checked'), djTest.Source.radio, 'Radio should match "radio" property value of source object.');
		
	},
	
	testSetH1Field : function () {
		
		var h1 = $('<h1 data-join="h1"></h1>');
		
		var form = $('<div></div>').append(h1);
		
		form.datajoin(djTest.Source);
		
		assertEqual(h1.text(), djTest.Source.h1, 'H1 field value should match "h1" property value of source object.');
		
	},
	
	testSetTextArea : function () {
		
		var textarea = $('<textarea data-join="textarea"></textarea>');
		
		var form = $('<div></div>').append(textarea);
		
		form.datajoin(djTest.Source);
		
		assertEqual(textarea.val(), djTest.Source.textarea, 'TextArea field value should match "textarea" property value of source object.');
		
	},
	
	testSetSelect : function () {
		
		var select = $('<select data-join="select"><option selected>default</option><option>one</option><option>two</option></select>');
		
		var form = $('<div></div>').append(select);
		
		form.datajoin(djTest.Source);
		
		assertEqual(select.val(), djTest.Source.select, 'Select field value should match "select" property value of source object.');
		
	},
	
	testSetMultiSelect : function () {
		
		var multiselect= $('<select data-join="multiselect" multiple><option>one</option><option>two</option><option>three</option><option>four</option><option>five</option></select>');
		
		var form = $('<div></div>').append(multiselect);
		
		form.datajoin(djTest.Source);
		
		assertEqual(multiselect.val(), djTest.Source.multiselect, 'MultiSelect field value should match "multiselect" property value of source object.');
		
	},
	
	testTextFieldChange : function() {
		
		var input = $('<input type="text" data-join="text" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		input.val('Lindsay').change();
		
		assertEqual(djTest.Source.text, 'Lindsay', '"text" property on source object should have been update by change event on text field.');
		
	},
	
	testCheckBoxChange : function() {
		
		var input = $('<input type="checkbox" data-join="checkbox" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		input.attr('checked', false).change();
		
		assertEqual(djTest.Source.checkbox, false, '"checkbox" property on source object should have been update by change event on checkbox.');
		
	},
	
	testRadioChange : function() {
		
		var input = $('<input type="radio" data-join="radio" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		input.attr('checked', false).change();
		
		assertEqual(djTest.Source.radio, false, '"radio" property on source object should have been update by change event on radiobutton.');
		
	},
	
	testH1FieldChange : function() {
		
		var h1 = $('<h1 data-join="h1"></h1>');
		
		var form = $('<div></div>').append(h1);
		
		form.datajoin(djTest.Source);
		
		h1.text('Not that great').change();
		
		assertEqual(djTest.Source.h1, 'Not that great', '"h1" property on source object should have been update by change event on H1 field.');
		
	},
	
	testTextAreaChange : function() {
		
		var textarea = $('<textarea data-join="textarea"></textarea>');
		
		var form = $('<div></div>').append(textarea);
		
		form.datajoin(djTest.Source);
		
		textarea.val('Not that great').change();
		
		assertEqual(djTest.Source.textarea, 'Not that great', '"textarea" property on source object should have been update by change event on TextArea field.');
		
	},
	
	testSelectChange : function() {
		
		var select = $('<select data-join="select"><option selected>default</option><option>one</option><option>two</option></select>');
		
		var form = $('<div></div>').append(select);
		
		form.datajoin(djTest.Source);
		
		select.val('two').change();
		
		assertEqual(djTest.Source.select, 'two', '"select" property on source object should have been update by change event on Select field.');
		
	},
	
	testMultiSelectChange : function() {
		
		var multiselect = $('<select data-join="multiselect" multiple><option>one</option><option>two</option><option>three</option><option>four</option><option>five</option></select>');
		
		var form = $('<div></div>').append(multiselect);
		
		form.datajoin(djTest.Source);
		
		multiselect.val(['one', 'three', 'five']).change();
		
		assertEqual(djTest.Source.multiselect, ['one', 'three', 'five'], '"multiselect" property on source object should have been update by change event on MultiSelect field.');
		
	},
	
	testSetTextFieldOverrideFieldAssign: function() {
		
		var input = $('<input type="text" data-join="text" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source, djTest.Override);
			
		assertEqual(input.val(), djTest.Source.text, 'Input field value should match "text" property value of source object.');
		
		assertEqual(input.css('text-align'), djTest.Source.textalign, 'Input text-aling should been center.');
	},
	
	testSetTextFieldOverrideSourceAssign: function() {
		
		var input = $('<input type="text" data-join="text" />');
		
		var form = $('<div></div>').append(input);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		input.val('Hello World!').change();
		
		assertEqual(djTest.Source.text, 'Hello World!', 'Source value should be "Hello World!".');
	},
	
	testSetSelectOverrideFieldAssign: function() {
		
		var select = $('<select data-join="select"><option selected value="0">default</option><option value="1">one</option><option value="2">two</option></select>');
	
		var form = $('<div></div>').append(select);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		assertEqual(select.val(), 1, 'Select value should be "1".');
	},
	
	testSetSelectOverrideSourceAssign: function() {
		
		var select = $('<select data-join="select"><option selected value="0">default</option><option value="1">one</option><option value="2">two</option></select>');
	
		var form = $('<div></div>').append(select);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		select.val(2).change();
		
		assertEqual(djTest.Source.select, 'two', 'Source value should be "two".');
	},
	
	testNestedSource : function () {
		
		var input = $('<input type="text" data-join="phone.cell" />');
		
		var form =  $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		assertEqual(input.val(), djTest.Source.phone.cell, 'Input should have same value as "phone.cell" property.');
		
	},	
	
	testNestedOverrideFieldAssign : function () {
		
		var input = $('<input type="text" data-join="phone.work" />');
		
		var form =  $('<div></div>').append(input);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		assertEqual(input.val(), '***-***-6789', 'Input should have same value should have been "***-***-6789".');
		
	},
	
	testNestedOverrideSourceAssign : function () {
		
		var input = $('<input type="text" data-join="phone.work" />');
		
		var form =  $('<div></div>').append(input);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		input.val('987-654-3210').change();
		
		assertEqual(djTest.Source.phone.work, '987-654-3210', 'Input should have same value should have been "987-654-3210".');
		
		assertEqual(input.val(), '***-***-3210', 'Input should have same value should have been "***-***-3210".');
		
	},
	
	testUnJoin : function(){
		
		var input = $('<input type="text" data-join="phone.cell" />');
		
		var form =  $('<div></div>').append(input);
		
		form.datajoin(djTest.Source);
		
		form.datajoin(djTest.Source, true);
		
		input.val('012-345-6789').change();
		
		assertNotEqual(input.val(), djTest.Source.phone.cell, 'Input should have a differnt value from property on source.');
	},
	
	testNoTagOverride : function() {
		
		var anchor = $('<a href="#" data-join="a">Click Me</a>');
		
		var form =  $('<div></div>').append(anchor);
		
		form.datajoin(djTest.Source);
			
		assertEqual(anchor.text(), djTest.Source.a, 'Anchor text should match property "a".');
		
		assertEqual(anchor.attr('href'), "#", 'Anchor href should be "#".');
	
	},
	
	testSetSelectOverrideTagFieldAssign: function() {
		
		var select1 = $('<select data-join="select1"><option selected value="0">default</option><option value="1">one</option><option value="2">two</option></select>');
		
		var select2 = $('<select data-join="select2"><option selected value="0">default</option><option value="1">A</option><option value="2">B</option></select>');
	
		var form = $('<div></div>').append(select1).append(select2);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		assertEqual(select1.val(), 1, 'Select value should be "1".');
		
		assertEqual(select2.val(), '2', 'Select value should be "2".');
	},
	
	testSetSelectOverrideTagSourceAssign: function() {
		
		var select1 = $('<select data-join="select1"><option selected value="0">default</option><option value="1">one</option><option value="2">two</option></select>');
		
		var select2 = $('<select data-join="select2"><option selected value="0">default</option><option value="1">A</option><option value="2">B</option></select>');
	
		var form = $('<div></div>').append(select1).append(select2);
		
		form.datajoin(djTest.Source, djTest.Override);
		
		select1.val(2).change();
		
		select2.val(1).change();
		
		assertEqual(djTest.Source.select1, 'two', 'Source value should be "two".');
		
		assertEqual(djTest.Source.select2, 'A', 'Source value should be "A".');
	}
};

